package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Checklist", "/checklist", "/ChecklistServlet", "/checklistServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class ChecklistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public ChecklistServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<ChecklistModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/checklistNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			ChecklistModel query = new ChecklistModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

			ChecklistModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("checklist", obj);
				getServletContext().getRequestDispatcher("/checklistEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			ChecklistModel query = new ChecklistModel();
			String strId = request.getParameter("id");
			int id = Integer.parseInt(strId);
			int idProject = 0;
			
			idProject = Integer.parseInt(request.getParameter("idProject"));
			String urlIdProject = "?idProject=" + idProject;
			query.setId(id);

			GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				
				//Redirect to checklist list of the project
 				response.sendRedirect("checklistList.jsp" + urlIdProject);
			} else {
				// TODO fail
				response.sendRedirect("checklistList.jsp" + urlIdProject);
			}
		} else if (mode.equalsIgnoreCase("view")) {

			ChecklistModel query = new ChecklistModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

			ChecklistModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("checklist", obj);
				getServletContext().getRequestDispatcher("/checklistView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			ChecklistModel obj = new ChecklistModel();

			String id_project = request.getParameter("id_project");
			if (id_project != null && !id_project.isEmpty()) {
				obj.setIdProject(Integer.parseInt(id_project));
			}

			// String object_type = request.getParameter("object_type");
			// obj.setObjectType(object_type);

			// String object_id = request.getParameter("object_id");
			// obj.setObjectId(object_id);

			String title = request.getParameter("title");
			obj.setTitle(title);

			// String code = request.getParameter("code");
			// obj.setCode(code);

			String description = request.getParameter("description");
			obj.setDescription(description);

			// String type = request.getParameter("type");
			// if (type != null && !type.isEmpty()) {
			// obj.setType(Integer.parseInt(type));
			// }
			//
			// String status = request.getParameter("status");
			// if (status != null && !status.isEmpty()) {
			// obj.setStatus(Integer.parseInt(status));
			// }
			//
			// String is_draft = request.getParameter("is_draft");
			// obj.setIsDraft(is_draft.equalsIgnoreCase("true")
			// || is_draft.equalsIgnoreCase("1"));
			//
			// String notification = request.getParameter("notification");
			// if (notification != null && !notification.isEmpty()) {
			// obj.setNotification(Integer.parseInt(notification));
			// }

			String id_author = request.getParameter("id_author");
			if (id_author != null && !id_author.isEmpty()) {
				obj.setIdAuthor(Integer.parseInt(id_author));
			}

			String id_approver = request.getParameter("id_approver");
			if (id_approver != null && !id_approver.isEmpty()) {
				obj.setIdApprover(Integer.parseInt(id_approver));
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			String urlIdProject = "?idProject=" + obj.getIdProjectToString();

			if (CommonBll.checkInputs(obj)) {
				GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					
					response.sendRedirect("checklistList.jsp" + urlIdProject);
				} else {
					// TODO : processing error here
					response.sendRedirect("checklistList.jsp" + urlIdProject);
				}
			} else {

				// invalid input
				request.setAttribute("checklist", obj);
				getServletContext().getRequestDispatcher("/checklistInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			ChecklistModel query = new ChecklistModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

			ChecklistModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// String object_type = request.getParameter("object_type");
				// obj.setObjectType(object_type);

				// String object_id = request.getParameter("object_id");
				// obj.setObjectId(object_id);

				String title = request.getParameter("title");
				obj.setTitle(title);

				// String code = request.getParameter("code");
				// obj.setCode(code);

				String description = request.getParameter("description");
				obj.setDescription(description);

				// String type = request.getParameter("type");
				// obj.setType(Integer.parseInt(type));
				//
				// String status = request.getParameter("status");
				// obj.setStatus(Integer.parseInt(status));
				//
				// String is_draft = request.getParameter("is_draft");
				// obj.setIsDraft(is_draft.equalsIgnoreCase("true")
				// || is_draft.equalsIgnoreCase("1"));
				//
				// String notification = request.getParameter("notification");
				// obj.setNotification(Integer.parseInt(notification));

				String id_project = request.getParameter("id_project");
				obj.setIdProject(Integer.parseInt(id_project));

				String id_author = request.getParameter("id_author");
				obj.setIdAuthor(Integer.parseInt(id_author));

				String id_approver = request.getParameter("id_approver");
				obj.setIdApprover(Integer.parseInt(id_approver));

				String urlIdProject = "?idProject=" + obj.getIdProjectToString();

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						
						response.sendRedirect("checklistList.jsp" + urlIdProject);
					} else {
						// TODO error
						response.sendRedirect("checklistList.jsp" + urlIdProject);
					}
				} else {
					request.setAttribute("checklist", obj);
					getServletContext().getRequestDispatcher("/checklistEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			ChecklistModel query = new ChecklistModel();
			String id = request.getParameter("id");
			int idProject = 0;
			idProject = Integer.parseInt(request.getParameter("idProject"));
			String urlIdProject = "?idProject=" + idProject;
			query.setId(Integer.parseInt(id));

			GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect("checklistList.jsp" + urlIdProject);
			} else {
				// TODO fail
				response.sendRedirect("checklistList.jsp" + urlIdProject);
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			ChecklistModel obj = new ChecklistModel();

			// TODO set name here

			GenericDal<ChecklistModel, ChecklistModelExt> dal = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}